/*
 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function checkTestResult(actual : RegExpExecArray | null, expected : RegExpExecArray | null) : int {
  if (actual == null && expected == null) {
     return 0;
  }
  if (actual == null || expected == null) {
     return 1;
  }

  if (actual.isCorrect != expected.isCorrect) {
     console.println("isCorrect field of actual is not equals to expected")
     console.println("actual.isCorrect : " + actual.isCorrect)
     console.println("expected.isCorrect : " + expected.isCorrect)
     return 1;
  }

  if (actual.index != expected.index) {
     console.println("index field of actual is not equals to expected")
     console.println("actual.index : " + actual.index)
     console.println("expected.index : " + expected.index)
     return 1;
  }

  if (actual.result.length != expected.result.length) {
    console.println("size of result array of actual is not equals to expected")
    console.println("actual size : " + actual.result.length);
    console.println("expected size : " + expected.result.length);
    return 1;
  }

  for (let i = 0; i < actual.result.length; i++ ) {
     if (!actual.result[i].equals(expected.result[i])) {
        console.println ("check index : " + i);
        console.println("content of result array from actual is not equals to expected");
        console.println("actual  : " + actual.result[i]);
        console.println("expected : " + expected.result[i]);
        return 1;
     }
  }

  for (let i = 0; i < actual.indices.length; i++) {
    let actInd = (Value.of(actual.indices) as ArrayValue).getElement(i).getData() as FixedArray<number>
    let expInd = (Value.of(expected.indices) as ArrayValue).getElement(i).getData() as FixedArray<number>
    for (let j = 0; j < actInd.length; j++) {
      if (actInd[j] != expInd[j]) {
        console.println("check index: " + i);
        console.println("content of result array from actual is not equals to expected");
        console.println("actual  : " + actInd[j]);
        console.println("expected : " + expInd[j]);
      }
    }
  }
  return 0;
}

function checkTestResult(actual: boolean, expected: boolean) : int {
   if (actual != expected) return 1;
   return 0;
}

const DBL_EPSILON : double = 1.0e-6;
const doubleEpsilon: double = 2.22045e-16
{%- if item.result_type_nature == "Float" or item.result_type == "Double" or item.result_type == "double" or item.result_type == "float"%}
function checkTestResult(actual : {{.item.result_type}}, expected : {{.item.result_type}}) : int {

{%- if item.result_type == "Float" or item.result_type == "Double" %}
    if(expected.isNaN()) {
    if (actual.isNaN())) return 0;
    else return 1;
  }
{% else %}
  if({{.item.result_type|capitalize}}.isNaN(expected)) {
    if ({{.item.result_type|capitalize}}.isNaN(actual)) return 0;
    else return 1;
  }
{% endif %}

  if(expected == {{.item.result_type|capitalize}}.POSITIVE_INFINITY) {
    if (actual == {{.item.result_type|capitalize}}.POSITIVE_INFINITY) return 0;
    else return 1;
  }

  if(expected == {{.item.result_type|capitalize}}.NEGATIVE_INFINITY) {
    if (actual == {{.item.result_type|capitalize}}.NEGATIVE_INFINITY) return 0;
    else return 1;
  }
  if (actual == expected) {
    return 0;
  }
  let diff : double;
  if (actual > expected) {
    diff = actual - expected;
  } else {
   diff = expected - actual;
  }
  if (diff < DBL_EPSILON) {
    return 0;
  } else {
    return 1;
}

}
{% endif %}

{% if item.result_nature == "array" %}
function checkTestResult(actual : {{.item.result_type}}, expected : {{.item.result_type}}) : int {
  console.print("\nACTUAL\n")
  for (let i: int = 0; i < actual.length; i++) {
     console.print(actual[i] + " ")
  }
  console.print("\nEXPECTED\n")
  for (let i: int = 0; i < expected.length; i++) {
     console.print(expected[i] + " ")
  }

  for (let i: int = 0; i < actual.length; i++) {
     if (actual[i] != expected[i])  return 1
  }
  console.println("\n****")
  return 0;
  }
{% endif %}
